package com.anysoft.web;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebView;

import com.anysoft.R;
import com.anysoft.event.TimeOutEvent;
import com.anysoft.me.base.BaseWebViewActivity;
import com.anysoft.utils.CommonUtil;
import com.anysoft.utils.HttpUtils;
import com.anysoft.utils.MentorLogger;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import butterknife.OnClick;

import static com.anysoft.HostConstant.HOST_WEB_URL_HEADER;

/**
 * Created by wwq on 2017/3/22.
 * 收益排行
 */

public class FundIncomeRankingActivity extends BaseWebViewActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isShowTitleBar(true);
        title.setText("收益排行");
        generateUrl("myearnings", HttpUtils.getCommonParams2StringByWeb(mActivity));
        if (CommonUtil.isNetWorkConnected(mActivity)) {
            setWebViewConfig(loadUrl);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    public void initUrl(String action, String param) {
        loadUrl = HOST_WEB_URL_HEADER + action + ".html?isFromApp=true&" + param;
        MentorLogger.Log(TAG, "loadUrl=" + loadUrl);
    }

    @Override
    public void onshouldOverrideUrlLoadingCallBack(WebView webview, String web_url) {
        MentorLogger.Log(TAG, "web_url=" + web_url);
        if (web_url.contains("fundDetail")) {
            String url = null;
            try {
                url = URLDecoder.decode(web_url, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            MentorLogger.Log(TAG, "url=" + url);
            String fundcode;
            String fundType;
            String mainTitle;
            String subTitle;
            Map<String, String> map = new HashMap<>();
            String[] params = url.split("\\?")[1].split("&");
            for (int i = 0; i < params.length; i++) {
                //System.out.println("params--" + params[i]);
                String key = params[i].split("=")[0];
                String value = "";
                try {
                    value = params[i].split("=")[1];
                } catch (Exception exception) {
                    MentorLogger.Log(TAG, "value为null");
                }
                map.put(key, value);
            }
            fundcode = map.get("fundCode");
            fundType = map.get("fundType");
            mainTitle = map.get("fundName") + "  " + map.get("fundCode");
            subTitle = map.get("fundstateDescription");

            mIntent.setClass(mActivity, FundDetailActivity.class);
            mIntent.putExtra("fundcode", fundcode);
            mIntent.putExtra("fundType", fundType);
            mIntent.putExtra("fundname", map.get("fundName"));
            mIntent.putExtra("mainTitle", mainTitle);
            mIntent.putExtra("subTitle", subTitle);
            startActivity(mIntent);
        } else {
            webview.loadUrl(web_url);
        }
    }

    @OnClick({R.id.leftButton, R.id.shareButton})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.leftButton:
                finish();
                break;
            case R.id.shareButton:
                //TODO 需要H5对应连接地址
//                SharePopup.Share(mActivity, "分享标题", "分享对应的网址", "分享的内容描述", "http://static.oschina.net/uploads/user/42/85712_100.jpeg?t=1405237014000");
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTimeOutCallBack(TimeOutEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
